import React, { Component } from 'react';
import { withRouter,NavLink} from 'react-router-dom';
import { Pagination } from 'antd';

import Unit from '../../unit';


class View extends Component {
    constructor(props){
        super(props);
        this.state={
            listData:[],
            loading:true,
            pPageCfg:{
                page_number:sessionStorage.getItem('currentPages')||1,
                page_size:10
            },
            setPageCfg:{
                currentPage:1,
                totalPage:10
            }
        }
        this.getPage = this.getPage.bind(this);
    }
    getPage(page,page_size=10){
        sessionStorage.setItem('currentPages', encodeURIComponent(page));
        const { pPageCfg }  = this.state;
        pPageCfg.page_number = page;
        pPageCfg.page_size = page_size;
        Unit.getApi2('/home/mediareports',pPageCfg,{}).then((res)=>{
            if(res.data.code===200){
                this.setState({
                    listData:res.data.data,
                    loading:false,
                    setPageCfg:{
                        currentPage:page,
                        totalPage:res.data.total
                    }
                })
            }else {
                //
            }
        })
    }
    lists(){
        const { listData } = this.state;
        const lists = listData.map((val,index)=>{
            return(
                <li key={val.id}>
                    <a href={val.jump_url}>{val.main_title}</a>
                    <span>{val.source}</span>
                    <span>{val.sourceDate}</span>
                </li>
            )
        })
        return lists;
    }
    componentDidMount(){
        // 拼多多
        const { pPageCfg } = this.state;
        setTimeout(()=>{
            this.getPage(pPageCfg.page_number);
        },3000)

    }
    render(){
        const { listData ,loading ,setPageCfg} = this.state;
        return(
            <React.Fragment>
            <NavLink exact to="/">首页</NavLink><br/>
            <NavLink to="/tabbak">tabbak</NavLink><br/>
            <NavLink to="/newspage">newspage</NavLink><br/>
            <NavLink to="/tab">tab</NavLink><br/>
            <NavLink to="/page">page</NavLink><br/>

                {
                        loading
                        ?
                        (<div className="loading_box"><div className="loading"></div></div>)
                        :
                        (listData.length!==0 ? <ul>{this.lists()}</ul>:<div className="no_result">小主，~什么也没有呀！</div>)
                }
                {
                    listData.length!==0 ? <Pagination onChange={this.getPage} defaultCurrent={setPageCfg.currentPage} total={setPageCfg.totalPage} /> : ''
                }
            </React.Fragment>
        )
    }

}








export default View;
